package com.sensePlusVR.activity.listener;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.sensePlusVR.utils.MyLog;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
	private static final String TAG = RecyclerItemClickListener.class.getSimpleName();
	
	public interface OnItemClickListener {
		void onItemClick(View view, int position);

		void onItemLongClick(View view, int position);
	}

	public RecyclerView recyclerView;
	private OnItemClickListener mListener;

	private GestureDetector mGestureDetector;

	public RecyclerItemClickListener(Context context,RecyclerView recyclerView, OnItemClickListener listener) {
		this.recyclerView=recyclerView;  
		this.mListener = listener;
		mGestureDetector = new GestureDetector(context,
				new GestureDetector.SimpleOnGestureListener() {
			
					@Override
					public boolean onSingleTapUp(MotionEvent e) {
						MyLog.d(TAG, "onSingleTapUp...");
						return true;
					}
					
					@Override
					public boolean onScroll(MotionEvent e1, MotionEvent e2,
							float distanceX, float distanceY) {
						MyLog.d(TAG, "onScroll : e1 : " + e1 + " e2 : " + e2 + " distanceX : " + distanceX + " distanceY : " + distanceY);
						return super.onScroll(e1, e2, distanceX, distanceY);
					}

					@Override
					public boolean onFling(MotionEvent e1, MotionEvent e2,
							float velocityX, float velocityY) {
						MyLog.d(TAG, "onFling : e1 : " + e1 + " e2 : " + e2 + " velocityX : " + velocityX + " velocityY : " + velocityY);
						return super.onFling(e1, e2, velocityX, velocityY);
					}

					@Override
					public void onShowPress(MotionEvent e) {
						MyLog.d(TAG, "onShowPress e.getX : " + e.getX() + "e.getY : " + e.getY());
						super.onShowPress(e);
					}



					@Override
					public boolean onDown(MotionEvent e) {
						MyLog.d(TAG, "onDown e.getX : " + e.getX() + "e.getY : " + e.getY());
						return super.onDown(e);
					}



					@Override
					public void onLongPress(MotionEvent e) {
						MyLog.d(TAG, "onLongPress111...");
						View childView = RecyclerItemClickListener.this.recyclerView.findChildViewUnder(
								e.getX(), e.getY());
						MyLog.d(TAG, "e.getX : " + e.getX() + "e.getY : " + e.getY());
						if (childView != null && mListener != null) {
							MyLog.d(TAG, "onLongPress222...");
							mListener.onItemLongClick(childView, RecyclerItemClickListener.this.recyclerView
									.getChildAdapterPosition(childView));
						}
					}
				});
	}

	@Override
	public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
		MyLog.d(TAG, "onInterceptTouchEvent111...");
		View childView = view.findChildViewUnder(e.getX(), e.getY());
		if (childView != null && mListener != null
				&& mGestureDetector.onTouchEvent(e)) {
			MyLog.d(TAG, "onInterceptTouchEvent222...");
			mListener.onItemClick(childView,
					view.getChildAdapterPosition(childView));
		}

		return false;
	}

	@Override
	public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
		MyLog.d(TAG, "onTouchEvent : ");
	}

	@Override
	public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
		MyLog.d(TAG, "onRequestDisallowInterceptTouchEvent : ");
	}
}
